package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.SheetInfoEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.text.NumberFormat;
import java.util.List;

public class EdcSetBinAdapter extends BaseQuickAdapter<EdcSetBinEntity, BaseViewHolder> {
    private int mMainqty = 0;

    public EdcSetBinAdapter(@Nullable List<EdcSetBinEntity> data) {
        super(R.layout.adapter_edc_bin,data);
    }

    public void setMainqty(int mMainqty) {
        this.mMainqty = mMainqty;
    }

    @Override
    protected void convert(BaseViewHolder helper, EdcSetBinEntity item) {
        helper.setIsRecyclable(false);
        helper.setText(R.id.tvName,item.getNAME())
                .setText(R.id.tvDesc,item.getDESCRIPTION())
                .setText(R.id.etValue,item.getVALUE())
                .setText(R.id.etComment,item.getCOMMENT());

        helper.setGone(R.id.etComment,false).setGone(R.id.tvCol,true);

        try {
            calculation(helper.getView(R.id.tvCol), Integer.parseInt(item.getVALUE()));
        } catch (NumberFormatException e) {
            calculation(helper.getView(R.id.tvCol), 0);
            e.printStackTrace();
        }

        EditText etValue = helper.getView(R.id.etValue);
        EditText etComment = helper.getView(R.id.etComment);
        //根据类型禁止输入内容
        if(item.getPROHIBIT().equals("Y")){
            etValue.setFocusable(false);
            etValue.setFocusableInTouchMode(false);
        }

        setEditListener(etValue,item,"Y",helper.getView(R.id.tvCol));
//        setEditListener(etComment,item,"N",helper.getView(R.id.tvCol));
    }

    private void calculation(TextView view, double binQTY) {
        if (mMainqty == 0) {
            return;
        }
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(3);
        String result = numberFormat.format(((float) (binQTY) / (float) mMainqty) * 100);
        view.setText(result + "%");
    }

    private void setEditListener(EditText editText, EdcSetBinEntity item, String type,TextView view) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0)
                {
                    if (type.equals("Y")){
                            try {
                                double value = Double.parseDouble(s.toString());
                                compare(value,item,editText);
                                calculation(view, Integer.parseInt(s.toString()));

                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                                calculation(view, 0);
                            }

                    }


                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (type.equals("Y")){
                    if(s.toString().equals("")){
                        item.setVALUE(null);
                    }
                    else if (s.toString().startsWith("0")){
                        String sub=s.toString().substring(1);
                        item.setVALUE(sub);
                    }else {
                        item.setVALUE(s.toString());
                    }

                }else {
                    item.setCOMMENT(s.toString());
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private void compare(double value, EdcSetBinEntity item, EditText editText){
        String maxValue = item.getUSL();
        String minValue = item.getLSL();
        if (item.getSPECTYPE().equals("NUMBER")){
            if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
                double maxValues = Double.parseDouble(maxValue);
                double minValues = Double.parseDouble(minValue);
                if(minValues<=value&&value<=maxValues) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
                double minValue1 = Double.parseDouble(minValue);
                if (value >= minValue1){
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
                double maxValue1 = Double.parseDouble(maxValue);
                if (value <= maxValue1){
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else {
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }
        }else if (item.getSPECTYPE().equals("PERCENT")){
            if (!FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
                double maxValues = mMainqty * Double.parseDouble(maxValue);
                double minValues = mMainqty * Double.parseDouble(minValue);
                if(minValues<=value&&value<=maxValues) {
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else if (FreeApi_StringUtils.isEmpty(maxValue)&&!FreeApi_StringUtils.isEmpty(minValue)){
                double minValue1 = mMainqty * Double.parseDouble(minValue);
                if (value >= minValue1){
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else if (!FreeApi_StringUtils.isEmpty(maxValue)&&FreeApi_StringUtils.isEmpty(minValue)){
                double maxValue1 = mMainqty * Double.parseDouble(maxValue);
                if (value <= maxValue1){
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
                }else{
                    editText.setTextColor(MyApp.app.getResources().getColor(R.color.red));
                }
            }else {
                editText.setTextColor(MyApp.app.getResources().getColor(R.color.black));
            }
        }

    }
}
